package com.pozu.new_retail.service.impl;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pozu.new_retail.dao.model.DrawAccount;
import com.pozu.new_retail.dao.mapper.CodeMapper;
import com.pozu.new_retail.dao.mapper.CodeSettingMapper;
import com.pozu.new_retail.dao.mapper.DrawAccountMapper;
import com.pozu.new_retail.dao.model.Code;
import com.pozu.new_retail.dao.model.CodeSetting;
import com.pozu.new_retail.service.CodeService;

@Service("codeService")
public class CodeServiceImpl implements CodeService {

	@Autowired
	private CodeMapper codeMapper;
	@Autowired
	private DrawAccountMapper drawAccountMapper;
	@Autowired
	private CodeSettingMapper codeSettingMapper;
	
	@Override
	public Code queryCodeByMap(HashMap<String, Object> map) {
		return codeMapper.queryCodeByMap(map);
	}

	@Override
	public int saveCode(Code code) {
		
		long time = System.currentTimeMillis();
		
		code.setCreatedTime(time);
		code.setUpdatedTime(time);
		
		//生成佣金提取账户
		if (null == drawAccountMapper.queryByUserId(code.getUserId())) {
			DrawAccount account = new DrawAccount();
			account.setUserId(code.getUserId());
			account.setCreatedTime(time);
			account.setUpdatedTime(time);
			drawAccountMapper.insertDrawAccount(account);	
		}
		
		return codeMapper.insertCode(code);
	}

	@Override
	public int updateCode(Code code) {
		code.setUpdatedTime(System.currentTimeMillis());
		return codeMapper.updateCode(code);
	}

	@Override
	public Code queryCodeObjByUserId(int appId,long userId) {
		
		HashMap<String,Object> hashMap = new HashMap<String,Object>();

		Code codeObj = null;
		//获取个人组团码信息
		if (appId == 10006) {
			//先判断是否有母码
			hashMap.put("appId", appId);
			hashMap.put("userId", userId);
			hashMap.put("level", 4);
			codeObj = queryCodeByMap(hashMap);
		}
		
		if(null == codeObj){
			//再判断该小程序下是否有总代理组团码
			hashMap = new HashMap<String, Object>();
			hashMap.put("appId", appId);
			hashMap.put("userId", userId);
			hashMap.put("level", 3);
			
			codeObj = queryCodeByMap(hashMap);
		}
		
		if (null == codeObj) {
			hashMap.remove("appId");
			hashMap.put("userId", userId);
			hashMap.put("level", 2);
			
			//再判断是否有超级组团码
			codeObj = queryCodeByMap(hashMap);
		}
		
		if(null == codeObj){
			//再判断该小程序下是否有微代理组团码
			hashMap = new HashMap<String, Object>();
			
			hashMap.put("appId", appId);
			hashMap.put("userId", userId);
			hashMap.put("level", 1);
			
			codeObj = queryCodeByMap(hashMap);
		}
		
		if(null == codeObj){
			//最后判断该是否有普通组团码
			hashMap = new HashMap<String, Object>();
			hashMap.remove("appId");
			hashMap.put("userId", userId);
			hashMap.put("level", 0);
			
			codeObj = queryCodeByMap(hashMap);
		}
		return codeObj;
	}

	@Override
	public Integer[] queryDiscountByCode(int appId, String code,long userId) {
		
		Integer[] discount = new Integer[2];
		
		HashMap<String,Object> hashMap = new HashMap<String,Object>();

		Code codeObj = null;
		//获取个人组团码信息
		if (appId == 10006) {
			//先判断是否有母码
			hashMap.put("appId", appId);
			hashMap.put("code", code);
			hashMap.put("level", 4);
			codeObj = queryCodeByMap(hashMap);
		}
		
		if(null == codeObj){
			//判断该小程序下是否有总代理组团码
			hashMap = new HashMap<String, Object>();
			
			hashMap.put("appId", appId);
			hashMap.put("code", code);
			hashMap.put("level", 3);
			
			codeObj = queryCodeByMap(hashMap);
		}
		
		if (null == codeObj) {
			hashMap = new HashMap<String,Object>();
			hashMap.remove("appId");
			hashMap.put("code", code);
			hashMap.put("level", 2);
			
			//先判断是否有超级组团码
			codeObj = queryCodeByMap(hashMap);
		}

		if(null == codeObj){
			//判断该小程序下是否有微代理组团码
			hashMap = new HashMap<String, Object>();
			
			hashMap.put("appId", appId);
			hashMap.put("code", code);
			hashMap.put("level", 1);
			
			codeObj = queryCodeByMap(hashMap);
		}
		
		if(null == codeObj){
			//判断该是否有普通组团码
			hashMap = new HashMap<String, Object>();
			hashMap.remove("appId");
			hashMap.put("code", code);
			hashMap.put("level", 0);
			
			codeObj = queryCodeByMap(hashMap);
		}
		
		if (null != codeObj) {
			
			if (codeObj.getUserId() == userId) {
				return null;
			}
			
			hashMap = new HashMap<String, Object>();

			hashMap.put("type", codeObj.getLevel());
			hashMap.put("appId", appId);
			CodeSetting codeSetting = codeSettingMapper.queryBaseInfoByMap(hashMap);
			
			discount[0] = codeObj.getLevel();
			
			if (codeObj.getLevel() == 3) {
				//总代理组团码
				discount[1] = codeObj.getReduction();
			}else {				
				discount[1] = codeSetting.getReduction();	
			}
			
			return discount;
		}
		
		return null;
	}

}
